<?php
/**
 * 系统消息模块
 * 
 * @version $Id$
 */

class Module_sysmsg
{
	/**
	 * 构造函数
	 */
	function __construct() {
		
	}

	/**
	 * 默认 action
	 */
	function index() {
		return $this->system();
	}
	
	/**
	 * 系统通知
	 */
	function system() {
		$smarty = loadSmarty();
		
		$uin = SessionInfo('uin');
		
		$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
		if($page < 1) {
			$page = 1;
		}
		
		$per_page = 10;
		$offset = ($page - 1) * $per_page;
		
		loadLib('SysMsg');
		$sysmsg_count = SysMsg::getTotalNum($uin , SysMsg::$TYPE_SYSTEM);
		
		$datas = SysMsg::getList($uin , SysMsg::$TYPE_SYSTEM , $offset , $per_page);
		
		// 处理数据
		$send_uin_list = array();
		$unread_list = array();
		foreach ($datas as $row) {
			if(!$row['isread']) {
				$unread_list[] = $row['id'];
			}
			
			if(!empty($row['data']['send_uin'])) {
				$send_uin_list[] = $row['data']['send_uin'];
			}
		}
		
		// 获取用户数据
		loadLib('User');
		$send_uin_info = User::getBaseInfo($send_uin_list);
		
		foreach ($datas as $k => $row) {
			if(!empty($row['data']['send_uin'])) {
				$datas[$k]['data']['send_uin_name'] = $send_uin_info[$row['data']['send_uin']]['name'];
				$datas[$k]['data']['send_uin_sex'] = $send_uin_info[$row['data']['send_uin']]['sex'];
			}
		}
		
		// 设置已读
		if(!empty($unread_list)) {
			SysMsg::setRead($uin , SysMsg::$TYPE_SYSTEM , $unread_list);
		} else {
			// 清空数字，保证数字准确
			SysMsg::deleteNum($uin , SysMsg::$TYPE_SYSTEM);
		}
		//print_r($datas);
		$smarty->assign('sysmsg_list' , $datas);
		$smarty->assign('sysmsg_count' , $sysmsg_count);
		$smarty->assign('per_page' , $per_page);

		return $smarty->fetch('sysmsg/system.tpl.php');
	}
	
	/**
	 * 发送好友申请
	 */
	function friend() {
		$smarty = loadSmarty();
		$uin = SessionInfo('uin');
		$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
		if($page < 1) {
			$page = 1;
		}
		
		$per_page = 10;
		$offset = ($page - 1) * $per_page;
		
		loadLib('SysMsg');
		$sysmsg_count = SysMsg::getTotalNum($uin , SysMsg::$TYPE_FRIEND);
		
		$datas = SysMsg::getList(SessionInfo('uin') , SysMsg::$TYPE_FRIEND , $offset , $per_page);
		
		// 处理数据
		$send_uin_list = array();
		foreach ($datas as $row) {
			$send_uin_list[] = $row['data']['send_uin'];
		}
		
		// 获取用户数据
		loadLib('User');
		$send_uin_info = User::getBaseInfo($send_uin_list);
		foreach ($datas as $k => $row) {
			$datas[$k]['data']['send_uin_name'] = $send_uin_info[$row['data']['send_uin']]['name'];
			$datas[$k]['data']['send_uin_sex'] = $send_uin_info[$row['data']['send_uin']]['sex'];
		}
		
		
		$smarty->assign('sysmsg_list' , $datas);
		$smarty->assign('sysmsg_count' , $sysmsg_count);
		$smarty->assign('per_page' , $per_page);
		
		return $smarty->fetch('sysmsg/friend.tpl.php');
	}
	
	/**
	 * 确认好友申请
	 */
	function friendconfirm() {
		$id = isset($_POST['id']) ? intval($_POST['id']) : null;
		
		if(empty($id)) {
			return View::error('参数错误');
		}
		
		// 查询是否有该好友请求
		loadLib('SysMsg');
		$data = SysMsg::getOne(SessionInfo('uin') , SysMsg::$TYPE_FRIEND , $id);
		if(empty($data)) {
			return View::error('该好友申请已不存在');
		}
		
		$to_uin = $id;
		
		// 把双方加为好友
		loadLib('Friend');
		$code = Friend::add(SessionInfo('uin') , $to_uin);
		
		if($code === 0) {
			// 删除该好友申请
			SysMsg::delete(SessionInfo('uin') , SysMsg::$TYPE_FRIEND , $id);
			
			// 发通知
			SysMsg::sendSystem($to_uin , array(
				'send_uin' => SessionInfo('uin'),
			) , 3);
			
			return View::success('添加好友成功');
		} else {
			return View::error('添加好友失败，请稍后再试' , $code);
		}
	}
	
	/**
	 * 删除消息
	 */
	function delete() {
		$type = isset($_POST['type']) ? intval($_POST['type']) : null;
		$id = isset($_POST['id']) ? intval($_POST['id']) : null;
		
		if(empty($id)) {
			return View::error('参数错误');
		}
		
		loadLib('SysMsg');
		$code = SysMsg::delete(SessionInfo('uin') , $type , $id);
		
		return $code === 0 ? View::success('删除好友申请成功') : View::error('删除好友申请失败，请稍后再试' , $code);
	}
}

# end of script
